package com.kplzj.maptest.map;

import android.annotation.SuppressLint;
import android.content.pm.ActivityInfo;
import android.content.res.AssetManager;
import android.graphics.PointF;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.kplzj.maptest.R;
import com.kplzj.maptest.map.customView.CarBean;
import com.kplzj.maptest.map.customView.CustomAreaMapView;
import com.kplzj.maptest.map.customView.CustomAreaView;
import com.kplzj.maptest.map.customView.XmlDataParser;

import org.json.JSONArray;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private CustomAreaMapView customMapView;
    private CustomAreaView customAreaView;
    CarPathView graphView;
    private List<PointF> points = new ArrayList<>();
    private List<CarBean> carBeans = new ArrayList<>();

    TextView btn_up;
    private int anInt = 4;
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 在Activity中强制横屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        setContentView(R.layout.activity_main);

        btn_up = findViewById(R.id.btn_up);
        customMapView = findViewById(R.id.customMapView);
        XmlDataParser xmlDataParser = new XmlDataParser();
        xmlDataParser.parseXmlFromAssets(this, "map3.xml");

//        customMapView.setPaths(xmlDataParser);

        try {
            getCarLocation(xmlDataParser,loadDataFromAssets("car3.txt"));

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        btn_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                anInt++;
                if(anInt > 12){

                    anInt =4;
                }
                try {
                    getCarLocation(xmlDataParser,loadDataFromAssets("car"+anInt+".txt"));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        customAreaView = findViewById(R.id.customAreaView);
        graphView = findViewById(R.id.graph_view);
    }

    private void getCarLocation(XmlDataParser xmlDataParser,String assets){

        points.clear();
        carBeans.clear();
        try {
//            JSONObject jsonObject = new JSONObject(loadDataFromAssets());
            JSONArray jsonArray = new JSONArray(assets);

            for (int i = 0; i < jsonArray.length(); i++) {

                CarBean carBean = new CarBean();
                float x = Float.parseFloat(jsonArray.getJSONObject(i).getString("x"));
                float y = Float.parseFloat(jsonArray.getJSONObject(i).getString("y"));
                points.add(new PointF(x, y));

                carBean.setX(x);
                carBean.setY(y);
                carBeans.add(carBean);
            }
            customMapView.setPaths(xmlDataParser,carBeans);
//            customAreaView.setPoints(carBeans);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    private String loadDataFromAssets(String fileName) throws IOException {

        AssetManager assetManager = getAssets();
        InputStream inputStream = null;
        StringBuilder stringBuilder = new StringBuilder();
        try{

            inputStream = assetManager.open(fileName);
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String line;

            while ((line = reader.readLine()) != null){
                stringBuilder.append(line);
            }
        }finally {
            if(inputStream != null){
                inputStream.close();
            }
        }

        return stringBuilder.toString();
    }

}